package com.kuang.controller;

import com.kuang.pojo.DataCollect;
import com.kuang.pojo.User;
import com.kuang.service.DataCollectService;
import com.kuang.service.UsageService;
import com.kuang.service.UserService;
import com.kuang.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.applet.resources.MsgAppletViewer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/login")
public class UserController {
    //Controller调Service层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @Autowired
    @Qualifier("UsageServiceImpl")
    private UsageService usageService;
    @Autowired
    @Qualifier("DataCollectServiceImpl")
    private DataCollectService dataCollectService;

    @RequestMapping("/goLogin")
    public String login(){
        return "login";
    }

    //通过username查询password
    @RequestMapping("/l3")
    public String text3(HttpSession session, Model model,String username , String password){
        System.out.println("param==>"+username);
        System.out.println("param==>"+password);
        String msg = null;

        if (username!=null){
            String selectUsername = userService.selectUsername(username);
            System.out.println(selectUsername);
            if (selectUsername.equals(username)){
                msg = "OK";
            }else {
                msg = "用户名有误";
                return "login";
            }
        }

        if (password!=null){
            String selectPassword = userService.selectPasswordByUsername(username);
            System.out.println(selectPassword);
            if (selectPassword.equals(password)){
                msg="OK";
            }else {
                msg = "密码有误";
                return "login";
            }
        }
        System.out.println(msg);
        if (msg=="OK"){

            //把用户的信息存在session中
            session.setAttribute("userLoginInfo",username);
            model.addAttribute("username",username);

            System.out.println(msg);
            return "redirect:/login/loginSuccess";
        }else {
            System.out.println(msg);
            return "login";
        }
    }

    @RequestMapping("/goOut")
    public String goOut(HttpSession session){

        session.removeAttribute("userLoginInfo");
        return "../../index";
    }

    @RequestMapping("/loginSuccess")
    public String list(Model model,HttpSession session){
        model.addAttribute("username",session.getAttribute("userLoginInfo"));
        List<Integer> usage_idList = usageService.selectUsage_idByUsername((String)session.getAttribute("userLoginInfo"));
        System.out.println(usage_idList);
        //未完成
        List<DataCollect> list = new ArrayList<DataCollect>();
        for (int i = 0; i <usage_idList.size() ; i++) {
            int usageId = usage_idList.get(i);
            int state = usageService.selectStateByUsage_id(usageId);
            if (state==0){ //未完成
                DataCollect dataCollect = dataCollectService.selectDataByUsage_id(usageId);
                //System.out.println(dataCollect);
                list.add(dataCollect);
            }
        }
        model.addAttribute("list",list);
        System.out.println(list);
        return "loginSuccess";
    }

    @RequestMapping("locationByUsageId")
    public String text2(Model model,int usage_id){
        model.addAttribute("usage_id",usage_id);
        System.out.println(usage_id);
        return "locationByUsageId";
    }

    @RequestMapping("/location")
    public String text1(HttpSession session,HttpServletResponse response,int usage_id) throws IOException {
        System.out.println("param==>"+usage_id);

        String begin = usageService.beginLocation(usage_id);
        System.out.println(begin);
        String arrival = usageService.arrivalLocation(usage_id);
        String location = dataCollectService.selectLocation(usage_id);

        List<String> list = new ArrayList<String>();
        list.add(begin);
        list.add(arrival);
        list.add(location);

        System.out.println(list);
        response.setContentType("text/javascript");
        response.getWriter().flush();
        response.getWriter().write(String.valueOf(list));
        response.getWriter().close();
        return JsonUtils.getJson(list);
    }
//查看历史订单
    @RequestMapping("toCompleteDate")
    public String text3(Model model,HttpSession session){
        model.addAttribute("username",session.getAttribute("userLoginInfo"));
        List<Integer> usage_idList = usageService.selectUsage_idByUsername((String)session.getAttribute("userLoginInfo"));
        System.out.println(usage_idList);
        //已完成
        List<DataCollect> list = new ArrayList<DataCollect>();

        for (int i = 0; i <usage_idList.size() ; i++) {
            int usageId = usage_idList.get(i);
            int state = usageService.selectStateByUsage_id(usageId);
            if (state==1){ //已完成
                DataCollect dataCollect = dataCollectService.selectDataByUsage_id(usageId);
                //System.out.println(dataCollect);
                list.add(dataCollect);
            }
        }
        model.addAttribute("list",list);
        System.out.println(list);
        return "loginSuccess";
    }
    //查询订单号
    @RequestMapping("queryUserDate")
    public String text4(int queryUsage_id,Model model){
        DataCollect dataCollect = dataCollectService.selectDataByUsage_id(queryUsage_id);
        List<DataCollect> list = new ArrayList<DataCollect>();
        list.add(dataCollect);

        if(dataCollect==null){
            model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);
        return "loginSuccess";
    }
}
